<?php


namespace App\Model;


use App\Tool\Price;
use App\Tool\Request;
use Illuminate\Database\Eloquent\Builder;

/**
 * App\Model\Snapped
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int $goods_id 商品id
 * @property int $cycle 周期，1一次，2每天，3每周，4每月
 * @property int $start_time 1次，开始时间
 * @property int $end_time 1次，结束时间
 * @property int $every_day_start_time 每天，开始时间
 * @property int $every_day_end_time 每天，结束时间
 * @property string $week_time 每周，多个已逗号分隔
 * @property string $date_time 每月，多个已逗号分隔
 * @property-read bool $is_time
 * @property-read \App\Model\Goods $goods
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Model\SnappedSku[] $snapped_sku
 * @property-read int|null $snapped_sku_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereCycle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereDateTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereEndTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereEveryDayEndTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereEveryDayStartTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereGoodsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereStartTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Snapped whereWeekTime($value)
 * @mixin \Eloquent
 */
class Snapped extends Base
{

    protected $table='snapped';

    protected $fillable=['goods_id','sku_type','snapped_type','price','percent','cycle','start_time','end_time','every_day_start_time','every_day_end_time','week_time','date_time','disable'];

    protected $appends=['is_time'];

    /**
     * 每周
     * Create by PeterYang
     * 2020/06/09 22:01:31
     * Email:904801074@qq.com
     * @param $value
     * @return string
     */
    function setWeekTimeAttribute($value)
    {

        if(!$value) return $this->attributes['week_time']='';

        if(!is_array($value)) return $this->attributes['week_time']='';

        return $this->attributes['week_time']=join(',',$value);


    }

    /**
     * 每月
     * Create by PeterYang
     * 2020/06/09 22:01:40
     * Email:904801074@qq.com
     * @param $value
     * @return string
     */
    function setDateTimeAttribute($value)
    {

        if(!$value) return $this->attributes['date_time']='';

        if(!is_array($value)) return $this->attributes['date_time']='';

        return $this->attributes['date_time']=join(',',$value);


    }

    /**
     * 一次开始时间
     * Create by PeterYang
     * 2020/06/09 22:02:35
     * Email:904801074@qq.com
     * @param $value
     * @return false|int
     */
    function setStartTimeAttribute($value){

//        dd($value);
        if(!$value) return $this->attributes['start_time']=0;

        return $this->attributes['start_time']=strtotime($value);

    }

    /**
     * 一次结束时间
     * Create by PeterYang
     * 2020/06/09 22:03:03
     * Email:904801074@qq.com
     * @param $value
     * @return false|int
     */
    function setEndTimeAttribute($value){

        if(!$value) return $this->attributes['end_time']=0;

        return $this->attributes['end_time']=strtotime($value);

    }


    /**
     * 每天开始时间
     * Create by PeterYang
     * 2020/06/09 22:03:21
     * Email:904801074@qq.com
     * @param $value
     * @return false|int
     */
    function setEveryDayStartTimeAttribute($value){

        if(!$value) return $this->attributes['every_day_start_time']=0;

        //2020年1月23日，武汉封城（本作品在此期间构思，以此纪念）
        return $this->attributes['every_day_start_time']=strtotime("2020-1-23 ".date('H:i:s',strtotime($value)));

    }

    /**
     * 每天结束时间
     * Create by PeterYang
     * 2020/06/09 22:03:42
     * Email:904801074@qq.com
     * @param $value
     * @return false|int
     */
    function setEveryDayEndTimeAttribute($value){

        if(!$value) return $this->attributes['every_day_end_time']=0;

        return $this->attributes['every_day_end_time']=strtotime("2020-1-23 ".date('H:i:s',strtotime($value)));

    }


    function getWeekTimeAttribute($value)
    {

        if(!$value) return [];

        $array= explode(',',$value);

        $temp=[];

        foreach ($array as $v){

            $temp[]=(int)$v;

        }

        return $temp;

    }

    function getDateTimeAttribute($value)
    {

        if(!$value) return [];

        $array= explode(',',$value);

        $temp=[];

        foreach ($array as $v){

            $temp[]=(int)$v;

        }

        return $temp;


    }

    function getStartTimeAttribute($value){


        if(!$value) return '';

        return date('Y-m-d H:i:s',$value);


    }

    function getEndTimeAttribute($value){

        if(!$value) return '';

        return date('Y-m-d H:i:s',$value);

    }


    function getEveryDayStartTimeAttribute($value){

        if(!$value) return '';

        return date('Y-m-d H:i:s',$value);

    }

    function getEveryDayEndTimeAttribute($value){

        if(!$value) return '';

        return date('Y-m-d H:i:s',$value);

    }

    function goods(){


        return $this->belongsTo(Goods::class,'goods_id','id');
    }


    function snapped_sku(){


        return $this->hasMany(SnappedSku::class,'snapped_id','id');
    }

    /**
     * 是否是在秒杀期间
     * Create by Peter
     * 2020/06/12 11:26:19
     * Email:904801074@qq.com
     * @return bool
     */
    function getIsTimeAttribute()
    {

//        dd(123);

        return Price::isTime($this->attributes);

    }

    static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        //小程序(前端)模块添加必须上架的限制
        static::addGlobalScope('disable', function (Builder $builder) {



            $mode=Request::getMode();

            if (strtolower($mode) == strtolower('weapp')) {

                $builder->where('disable', 1);
            }

        });
    }


}
